//
//  KLSGCategoryViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGCategoryViewController.h"
#import "KLSGCategoryTool.h"
#import "KLSGCategoryCollectionViewCell.h"
#import "KLSGCategoryModel.h"
#import "KLSGKindViewController.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "KLSGDataFail.h"
#import "DDIndicator.h"

@interface KLSGCategoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, CHTCollectionViewDelegateWaterfallLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) KLSGCategoryTool *tool;
@property (nonatomic, strong) NSMutableArray *categoryArray;
// 加载失败，用于刷新的视图
@property (nonatomic, strong) KLSGDataFail *failView;
// 加载指示器
@property (nonatomic, strong) DDIndicator *indicator;


@end

@implementation KLSGCategoryViewController

// 懒加载
- (KLSGDataFail *)failView
{
    if (!_failView) {
        _failView = [[KLSGDataFail alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _failView;
}

- (KLSGCategoryTool *)tool
{
    if (!_tool) {
        _tool = [[KLSGCategoryTool alloc] init];
    }
    return _tool;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self subView];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = NO;
    if (self.categoryArray.count == 0) {
        [self setupData];
    }
    
}

- (void)setupData
{
    [self.failView hidden];
    // 启动指示器
    [self.indicator startAnimating];
    [self.tool setupDataWithUrl:kCategoryUrl block:^(NSDictionary *dic) {
        // 停止指示器
        [self.indicator stopAnimating];
        NSArray *lists = dic[@"list"];
            self.categoryArray = [NSMutableArray array];
            for (NSDictionary *dic in lists) {
                KLSGCategoryModel *model = [[KLSGCategoryModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.categoryArray addObject:model];
            }
        [self.collectionView reloadData];
    } failDownloadWithBlock:^{
        // 停止指示器
        [self.indicator stopAnimating];
        [self.failView netRequestFailWithBlock:^{
            [self setupData];
        }];
        [self.collectionView addSubview:self.failView];
    }];
    
}

- (void)subView
{
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.minimumInteritemSpacing = 13;
//    layout.minimumLineSpacing = 13;
//    layout.itemSize = CGSizeMake((kScreenWidth - 60) / 3, 140);
//    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    KLSGWaterFlow *waterFlow = [[KLSGWaterFlow alloc] init];
//    waterFlow.delegate = self;
//    waterFlow.columsCount = 2;
//    waterFlow.sectionInset =UIEdgeInsetsMake(8, 8, 8, 8);
    CHTCollectionViewWaterfallLayout *fallLayout = [[CHTCollectionViewWaterfallLayout alloc] init];
    fallLayout.minimumColumnSpacing = 5;
    fallLayout.minimumInteritemSpacing = 5;
    fallLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:fallLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];
    
    // 注册cell
    [self.collectionView registerClass:[KLSGCategoryCollectionViewCell class] forCellWithReuseIdentifier:@"categoryCell"];
    
    // 加载指示器
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.indicator.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
    [self.view addSubview:self.indicator];
    
}

#pragma mark -实现UICollectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.categoryArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"categoryCell" forIndexPath:indexPath];
    KLSGCategoryModel *model = self.categoryArray[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.image = [UIImage imageNamed:[NSString stringWithFormat:@"category%d.png",[model.orderNum intValue]]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGCategoryModel *model = self.categoryArray[indexPath.row];
    KLSGKindViewController *kindVC = [[KLSGKindViewController alloc] init];
    [self addChildViewController:kindVC];
    kindVC.Id = model.Id;
    kindVC.titles = model.title;
    [self.navigationController pushViewController:kindVC animated:YES];
}

#pragma mark 返回瀑布流的高度

//-(CGFloat)waterFlow:(KLSGWaterFlow *)waterFlow heightForWidth:(CGFloat)width indexPath:(NSIndexPath *)indexPath
//{
//    KLSGCategoryCollectionViewCell *cell = (KLSGCategoryCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
//    return cell.height;
//}

// CHTCollectionViewDelegateWaterfallLayout的代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGCategoryModel *model = self.categoryArray[indexPath.row];
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"category%d", [model.orderNum intValue]]];
    CGFloat height = (kScreenWidth / 3 - 20) / image.size.width * image.size.height;
//    CGSize size = CGSizeMake((kScreenWidth / 3 - 20), height);
    CGSize size = CGSizeMake((kScreenWidth / 3 - 20), height);
    return size;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return ;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
